package com.yanqu.road.server.cmd.monopolymarket.rank;

import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.MonopolyMarketRankActivityMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Description 跨服通知区服 加排行榜 数值    加成功后回调给跨服 跟他说加成功了
 * @Author cwq
 * @Data 2021/9/26 18:02
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_NOTIFY_ADD_RANK_VALUE_FROM_CROSS, desc = "通知区服加排行榜数值 跨服返回区服 协议")
public class MonopolyMarketAddRankValueFromCrossCmd implements Command {

    public static volatile long maxId = 0;

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg msg = MonopolyMarketProto.MonopolyMarketAddRankValueToGameServerMsg.parseFrom(packet.getBytes());
        MonopolyMarketProto.MonopolyMarketAddRankValueToCrossReqMsg.Builder toCrossMsg = MonopolyMarketProto.MonopolyMarketAddRankValueToCrossReqMsg.newBuilder();
        long roundMaxId = 0;
        for (MonopolyMarketProto.MonopolyMarketAddRankValueEntity item :  msg.getAddRankListList()){
            if(maxId != 0 && item.getId() <= maxId){
                MonopolyMarketMgr.logger.info("出现重复下发给排行榜加值的情况。id:"+item.getId());
                continue;
            }
            long userId = item.getUserId();
            long addValue = item.getAddValue();
            long id = item.getId();
            long startTime = item.getStartTime();
            long time = item.getTime();
            int type = item.getType();
            // 加入冲榜
            MonopolyMarketRankActivityMgr.addRankValue(userId, addValue,time,startTime,type);
            toCrossMsg.addId(id);
            roundMaxId = Math.max(roundMaxId,item.getId());
        }
        if(toCrossMsg.getIdCount() > 0){
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_CROSS_MONOPOLY_MARKET_ADD_RANK_VALUE_SUCCESS, toCrossMsg));
        }
        maxId = Math.max(maxId,roundMaxId);
    }
}
